package com.qingpeng.pay.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.qingpeng.pay.R;
import com.qingpeng.pay.api.Host;
import com.qingpeng.pay.api.RedBag;
import com.qingpeng.pay.base.BaseActivity;
import com.qingpeng.pay.beans.NullBean;
import com.qingpeng.pay.okhttp.RetrofitCreateHelper;
import com.qingpeng.pay.utils.StringUtils;
import com.qingpeng.pay.utils.ToastUtils;
import com.qingpeng.pay.views.TitleBar;

import butterknife.BindView;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;


public class PayPassowrdActivity extends BaseActivity<NullBean> {
    @BindView(R.id.title_bar)
    TitleBar titleBar;
    @BindView(R.id.et_1)
    EditText et1;
    @BindView(R.id.et_2)
    EditText et2;
    @BindView(R.id.et_3)
    EditText et3;
    @BindView(R.id.et_4)
    EditText et4;
    @BindView(R.id.et_5)
    EditText et5;
    @BindView(R.id.et_6)
    EditText et6;
    @BindView(R.id.btn_ok)
    Button btnOk;
    int type;
    String code;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_pay_pwd;
    }

    @Override
    protected int getRootViewId() {
        return R.id.layout_root;
    }

    @Override
    protected int getRefreshId() {
        return 0;
    }

    @Override
    protected int getListViewId() {
        return 0;
    }

    @Override
    protected void init(Bundle savedInstanceState) {
        super.init(savedInstanceState);
        Intent intent = getIntent();
        type = intent.getIntExtra("type", 1);
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        super.initView(savedInstanceState);
        titleBar.getLlLeft().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                PayPassowrdActivity.this.finish();
            }
        });
        String title = "";
        if (type == 1) {
            title = "设置支付密码";
        } else {
            title = "重置支付密码";
        }
        titleBar.setTitle(title);
        setFocus();
    }

    private void setFocus() {
        doFocus(et1, et2);
        doFocus(et2, et3);
        doFocus(et3, et4);
        doFocus(et4, et5);
        doFocus(et5, et6);
    }

    private void doFocus(final EditText input, final EditText next) {
        input.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                String s1 = input.getText().toString();
                if (s1.length() == 1) {
                    next.requestFocus();
                }
            }
        });
    }

    @OnClick(R.id.btn_ok)
    public void onViewClicked() {
        String s1 = et1.getText().toString();
        String s2 = et2.getText().toString();
        String s3 = et3.getText().toString();
        String s4 = et4.getText().toString();
        String s5 = et5.getText().toString();
        String s6 = et6.getText().toString();
        if (StringUtils.isEmpty(s1) || StringUtils.isEmpty(s2) || StringUtils.isEmpty(s3) || StringUtils.isEmpty(s4) || StringUtils.isEmpty(s5) || StringUtils.isEmpty(s6)) {
            com.qingpeng.pay.utils.ToastUtils.showToast(PayPassowrdActivity.this, "请正确输入支付密码");
            return;
        }
        String pswStr = s1 + s2 + s3 + s4 + s5 + s6;
        int pwd = Integer.parseInt(pswStr);
        if (type == 1) {
            setPwd(pwd);
        } else {
            resetPwd(pwd);
        }
    }

    public void setPwd(int pwd) {
        call = RetrofitCreateHelper.createApi(RedBag.class, Host.HOST).pay_pwd(pwd + "");
        call.enqueue(new Callback<NullBean>() {
            @Override
            public void onResponse(Call<NullBean> call, Response<NullBean> response) {
                NullBean body = response.body();
                if (body.getStatus() != 1) {
                    ToastUtils.showToast(PayPassowrdActivity.this, body.getInfo());
                    return;
                }
                ToastUtils.showToast(PayPassowrdActivity.this, body.getInfo());
                PayPassowrdActivity.this.finish();
            }

            @Override
            public void onFailure(Call<NullBean> call, Throwable t) {
                ToastUtils.showToast(PayPassowrdActivity.this, "网络请求错误");
            }
        });
    }

    public void resetPwd(int pwd) {
        call = RetrofitCreateHelper.createApi(RedBag.class, Host.HOST).pay_pwd("" + pwd);
        call.enqueue(new Callback<NullBean>() {
            @Override
            public void onResponse(Call<NullBean> call, Response<NullBean> response) {
                NullBean body = response.body();
                if (body == null) {
                    ToastUtils.showToast(PayPassowrdActivity.this, "请求错误");
                    return;
                }
                if (body.getStatus() != 1) {
                    ToastUtils.showToast(PayPassowrdActivity.this, body.getInfo());
                    return;
                }
                ToastUtils.showToast(PayPassowrdActivity.this, body.getInfo());
                PayPassowrdActivity.this.finish();
            }

            @Override
            public void onFailure(Call<NullBean> call, Throwable t) {
                ToastUtils.showToast(PayPassowrdActivity.this, "网络请求错误");
            }
        });
    }
}
